package com.yty.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.yty.system.config.Producer;
import com.yty.system.entity.AccountInfo;
import com.yty.system.entity.DeDuplication;
import com.yty.system.entity.Tree;
import com.yty.system.mapper.AccountInfoMapper;
import com.yty.system.mapper.DeDuplicationMapper;
import com.yty.system.service.IAccountInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yegang
 * @since 2022-06-21
 */
@Service
@Slf4j
public class AccountInfoServiceImpl extends ServiceImpl<AccountInfoMapper, AccountInfo> implements IAccountInfoService {

    @Autowired
    private AccountInfoMapper accountInfoDao;
    @Autowired
    private DeDuplicationMapper deDuplicationMapper;

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    @Autowired
    private Producer producer;

    @Value("${rocketmq.topic}")
    private String TOPIC;

    @Value("${rocketmq.txGroup}")
    private String PRODUCER_GROUP;


    /**
     * 更新帐号余额-发送消息
     * producer向MQ Server发送消息
//     * @param accountInfo
     */
    @Override
    @Transactional
    public void transfer(AccountInfo accountInfo) throws Exception{
        // 1 先保存本地事务信息
        AccountInfo a = accountInfoDao.selectOne(new QueryWrapper<AccountInfo>().lambda().eq(AccountInfo::getAccountNo,accountInfo.getAccountNo()));
        if (a == null){
            throw new RuntimeException("账户不存在！");
        }
        a.setAccountBalance(a.getAccountBalance() + accountInfo.getAmount());
        accountInfoDao.updateById(a);
        DeDuplication deDuplication = new DeDuplication();
        // 2 记录操作日志
        deDuplication.setCreateTime(LocalDateTime.now())
                .setId(UUID.randomUUID().toString().replace("-",""));
        // 3 发送消息到mq
        accountInfo.setDeDuplicationId(deDuplication.getId());
//        SendResult result = producer.syncSend(TOPIC,JSONObject.toJSONString(accountInfo));
        // 4 记录消息id
//        deDuplication.setMessageId(result.getMsgId());
//        deDuplicationMapper.insert(deDuplication);
//        log.info("结束更新本地事务，事务号：{}",result.getMsgId());
    }

    @Override
    public List<Tree> tree() {
        List<Tree> list = getTreeVo();

        for (Tree tree : list) {
            if (tree.getId().equals(tree.getPId())) {
                list.remove(tree);
                break;
            }
        }

        TreeMap map = new TreeMap();

        return list;
    }

    private void getaa() {

    }

    private List<Tree> getTreeVo() {
        List<Tree> list = new ArrayList<>();
        list.add(getT(1, "阿里巴巴", 0));
        list.add(getT(2, "财务部", 1));
        list.add(getT(21, "张三", 2));
        list.add(getT(22, "李四", 2));
        list.add(getT(23, "网二", 2));
        list.add(getT(24, "麻子", 2));
        list.add(getT(3, "综合部", 1));
        list.add(getT(31, "猪八戒", 3));
        list.add(getT(32, "孙悟空", 3));
        list.add(getT(4, "技术部00000", 1));
        list.add(getT(41, "技术1部", 4));
        list.add(getT(42, "技术2部", 4));
        list.add(getT(421, "刘德华", 42));
        list.add(getT(422, "张雪玉", 42));
        list.add(getT(423, "孙马玉", 42));
        list.add(getT(424, "李连杰", 42));
        return list;
    }

    private Tree getT(Integer id, String name, Integer pid) {
        Tree t = new Tree();
        t.setId(id).setTreeName(name).setPId(pid);
        return t;
    }
}
